package com.ksv.qlsv;

import com.ksv.lop.*;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class LopHocEditActivity extends Activity {
	private TextView txt_id;
	private TextView txt_name;
	private Button btn_save;
	private Button btn_cancel;
	private Spinner com_khoa;
	
	public static LopHoc lop = new LopHoc();
	public static int iAddEditView = 1;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogedit);            
        this.setTitle(getResources().getString(R.string.lop));
        btn_save = (Button)findViewById(R.id.btn_save);
        btn_save.setText(getResources().getString(R.string.luu));
		btn_cancel = (Button)findViewById(R.id.btn_cancel);
		btn_cancel.setText(getResources().getString(R.string.huy));
        txt_id = (TextView)findViewById(R.id.txt_id);
        txt_name = (TextView)findViewById(R.id.txt_name);        
        com_khoa = (Spinner)findViewById(R.id.com_khoa);
        com_khoa.setVisibility(View.VISIBLE);
        loadListKhoa();
        if(iAddEditView == 1){
        	lop.setMaLop(ConnectDB.getLopHocMax());
        	txt_id.setText(String.valueOf(lop.getMaLop()));
        	txt_id.setEnabled(false);
        	txt_name.setText("");
        	txt_name.setEnabled(true);
        }
        else if(iAddEditView == 2){
        	txt_id.setText(String.valueOf(lop.getMaLop()));        
        	txt_id.setEnabled(false);
        	txt_name.setText(lop.getTenLop());
        	com_khoa.setSelection(getPosiontionListKhoa());
        }
        else{
        	txt_id.setText(String.valueOf(lop.getMaLop()));        
        	txt_id.setEnabled(false);
        	txt_id.setEnabled(false);        	
        	txt_name.setEnabled(false);
        	btn_save.setEnabled(false);
        	txt_name.setText(lop.getTenLop());
        	com_khoa.setEnabled(false);
        	com_khoa.setSelection(getPosiontionListKhoa());
        }
        onClickSave();
        onClickCancel();
        onSelectedItem();
    }

	private int getPosiontionListKhoa() {
		for (int i = 0; i < com_khoa.getCount(); i++) {
			try{
				Cursor c = (Cursor) com_khoa.getItemAtPosition(i);
				if(c.getInt(c.getColumnIndex(ConnectDB.makhoa)) == lop.getMaKhoa()){
					return i;
				}
			}
			catch(Exception Ex){
				
			}
		}
		return 0;
	}

	private void loadListKhoa() {
		try{		    
			com_khoa.setAdapter(null);		       
    	    Cursor c = ConnectDB.getAllKhoa();
    	    String [] from = new String []{ConnectDB.tenkhoa, "_id"};
    		int [] to = new int [] {R.id.txt_name};
    		SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.listrow_khoa,c,from,to);
    		com_khoa.setAdapter(sca);
    	}
    	catch(Exception ex)
		{
    		
		}
	}

	private void onSelectedItem() {
		com_khoa.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				lop.setMaKhoa((int)id);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void onClickSave() {
		// TODO Auto-generated method stub		
		btn_save.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(iAddEditView == 1){
					lop.setTenLop(txt_name.getText().toString());
					ConnectDB.AddLopHoc(lop);
					LopHocEditActivity.this.finish();
				}
				else if(iAddEditView == 2){
					lop.setTenLop(txt_name.getText().toString());
					ConnectDB.updateLopHoc(lop);
					LopHocEditActivity.this.finish();
				}
				else{
					LopHocEditActivity.this.finish();
				}
			}
		});
	}

	private void onClickCancel() {
		// TODO Auto-generated method stub
		btn_cancel.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub				
				LopHocEditActivity.this.finish();
			}
		});		
	}
	
	
}
